package Components;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JPanel;

public class BattleResult extends JPanel implements Runnable{
	
	private String DIR = "picture/playground_pics/";
	private BufferedImage background;
	private boolean type;
	private BufferedImage pics;
	private JDialog motherDialog;
	
	public BattleResult(int my, int enemy, JDialog jd) {		
		try {
		this.motherDialog = jd;	
		this.background = ImageIO.read(new File(DIR+"bgm.png"));
	
		if(my > enemy){
			type = true;			
			pics = ImageIO.read(new File(DIR+"win.png"));
		} else {
			type = false;
			pics = ImageIO.read(new File(DIR+"lose.png"));
		}
		
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Nie poprawny odczyt obrazkow do wygrania");
		}	
		System.out.println(this.getWidth() +" "+this.getHeight());
		this.setVisible(true);
		this.setSize(pics.getWidth(), pics.getHeight());
		
	}

	@Override
	public void run() {
		this.repaint();
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		motherDialog.setVisible(false);
		motherDialog.disable();
		this.removeAll();
		
		
	}
	
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		
		if(!type) {
		g2.drawImage(background, 0, 0, null);
		} 		
		
		g2.drawImage(pics, 0,0,null);
		
	}
	
	public void start() {
		Thread tr = new Thread(this);
		tr.start();
	}

}
